<?php

namespace viewPost;
require_once 'model/PrintPictures.php';

class ViewPost{
	private static $admin = "Admin";
	private $printPic;
	public function __construct(){
		$this->printPic = new \printPictures\PrintPictures();
	}
	//output the images
	public function postPictures(){
		$files = $this->printPic->getFiles();
		$colCnt=0;
		$images = "";
		for ($i=0; $i<count($files); $i++)
		    {
		    	if(stristr($files[$i], 'Thumbs.db') == FALSE){
	    			$comment = $this->getComments($files[$i]);
					$num = $files[$i];	
					$images .= '<div class="photoPosts">
				    				<a name="'.$i.'" href="'.$num.'"target="_blank"><img src="'.$num.'" width="500" height="250"/></a>';
					$images .= $this->deleteButton($files[$i]);
					$images .=	'<div class="comments">'. $comment .'</div>
								<form name="'.$files[$i].'" class="form-inline" action="index.php?attempt" method="post" role="form">
								    	<label class="sr-only" for="Username">Username</label>
								    	<input type="text" class="form-control" name="comments" id="text" placeholder="Skriv kommentar här!">
								    	<button name="postText" type="submit" value="'.$files[$i].'" class="btn btn-default">Kommentera!</button>
							    	</form>
								</div>';
				}
		    	
		    }
		return $images;
	}
	//output deletebutton if admin is logged in
	private function deleteButton($imgName){
		$username = $this->printPic->getUsername();
		
		$deleteButton = "";	
		if($username == self::$admin){
			$deleteButton .= "<form class='form-inline' action='index.php?attempt' method='post' role='form'>
								<button class='deleteImg' name='deleteImg' value='".$imgName."' 
								type='submit'>Delete Image</button>
							</form>";
		}
		return $deleteButton;
	}
	//output comments and edit and delete button
	public function getComments($imgName){
			$result = $this->printPic->getDbResults();
			$username = $this->printPic->getUsername();
			$comment = "";
			while($row = mysqli_fetch_array($result)){
				if($imgName == $row['Id']){
					$comment .="<div class='commentBox'>";
					
					if(isset($username) && $username == $row['User'] || $username == self::$admin){
						$comment .= 
							" 
							<form class='form-inline' action='index.php?attempt' method='post' role='form'>
								<button class='deleteComment' name='deleteComment' value='".$row['UniqueId']."' 
								type='submit'>Delete</button>
							</form>
							<div id='commentedit".$row['UniqueId']."'>
							
								<a class='edit' onclick='toggle_visibility(".$row['UniqueId'].")'>
								<p>Edit</p>
								</a>						
								<div id='".$row['UniqueId']."' class='alist' style='display:none;'>
									<form class='form-inline' action='index.php?attempt' method='post' role='form'>
					    				<input type='text' name='newComment' id='editText' value='".$row['comments']."' placeholder='Redigera Inlägg'>
										<button id='editComment' name='editComment' value='".$row['UniqueId']."' 
										type='submit'>Redigera</button>
									</form>
								</div>
							</div>
							";
					}
					$comment .= '<p>'.$row['comments']."   </br>/".$row['User'].'</p>';
					$comment .="</div>";
				}
	  		}
	  return $comment;
	}
}